package com.aptoto.thrift.config;

import java.util.HashMap;
import java.util.Map;
/**
 * 记录thrift IDL(interface define language) 配置(*.thrift)文件中的struct
 * thrift 官方定义：Field ::= FieldID? FieldReq? FieldType Identifier ('= ConstValue)? XsdFieldOptions ListSeparator?
 * @author Orion
 */
public class ThriftStructFieldConfig {
	
	private static final Map<String, Map<Integer, ThriftStructFieldConfig>> StructConfig = new HashMap<String, Map<Integer, ThriftStructFieldConfig>>();
	
	public synchronized static void addThriftStructFieldConfig(String structIdentifier, ThriftStructFieldConfig config) {
		Map<Integer, ThriftStructFieldConfig> fieldMap;
		boolean containStruct = ThriftStructFieldConfig.StructConfig.containsKey(structIdentifier);
		
		if (containStruct)
			fieldMap = ThriftStructFieldConfig.StructConfig.get(structIdentifier);
		else
			fieldMap = new HashMap<Integer, ThriftStructFieldConfig>();
		
		fieldMap.put(config.fieldID, config);
		ThriftStructFieldConfig.StructConfig.put(structIdentifier, fieldMap);
	}
	
	public static ThriftStructFieldConfig getThriftStructFieldConfig(String structIdentifier, int fieldID) {
		boolean containStruct = ThriftStructFieldConfig.StructConfig.containsKey(structIdentifier);
		if ( !containStruct )
			return null;
		
		boolean containField = ThriftStructFieldConfig.StructConfig.get(structIdentifier).containsKey(fieldID);
		if ( !containField )
			return null;
		
		return ThriftStructFieldConfig.StructConfig.get(structIdentifier).get(fieldID);
	}
	
	public final int defineIndex; //定义顺序，从0开始
	public final int fieldID; //field id
	public final byte fieldType; //thrift type, @see: org.apache.thrift.protocol.TType
	public final String indentifier; //field name
	public ThriftStructFieldConfig(int defineIndex, int fieldID, byte fieldType, String indentifier) {
		super();
		this.defineIndex = defineIndex;
		this.fieldID = fieldID;
		this.fieldType = fieldType;
		this.indentifier = indentifier;
	}
}
